package videolib;

import Managers.CategoryManagerImpl;
import Managers.LibraryManagerImpl;
import Managers.MediumManagerImpl;
import Tools.DatabaseException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * Created by Václav Bukovský on 10. 6. 2014.
 * author: Václav Bukovský
 * version: 2014-06-10
 */
@WebListener
public class Listener implements ServletContextListener {

    public static final Logger logger = LoggerFactory.getLogger(Listener.class);

    private String login;
    private String password;
    private String categoryCollection;
    private String mediumCollection;

    private MediumManagerImpl mediumManager;
    private CategoryManagerImpl categoryManager;
    private LibraryManagerImpl libraryManager;

    /**
     * Method that ensures parsing properties and data from db.properties.
     * (For example, the login information for the database connection.)
     */
    private void parseProperty() {
        Properties properties = new Properties();

        try (InputStream is = getClass().getClassLoader().getResourceAsStream("db.properties")) {
            properties.load(is);
        } catch (IOException ex) {
            logger.debug("Error reading property file.");
        }

        this.login = properties.getProperty("user");
        this.password = properties.getProperty("password");
        this.mediumCollection = properties.getProperty("mediumCollection");
        this.categoryCollection = properties.getProperty("categoryCollection");

    }

    /**
     * Method creates new instance of medium manager in servlet context
     * @return medium manager
     * @throws DatabaseException when error occurs in process
     */
    private MediumManagerImpl mediumManagerSetUp() throws DatabaseException {

        mediumManager = new MediumManagerImpl(mediumCollection, login, password);
        return mediumManager;
    }

    /**
     * Method creates new instance of category manager in servlet context
     * @return category manager
     * @throws DatabaseException when error occurs in process
     */
    private CategoryManagerImpl categoryManagerSetUp() throws DatabaseException {

        categoryManager = new CategoryManagerImpl(categoryCollection, login, password);
        return categoryManager;
    }

    /**
     * Method creates new instance of library manager in servlet context
     * @return library manager
     * @throws DatabaseException when error occurs in process
     */
    private  LibraryManagerImpl libraryManagerSetUp() throws DatabaseException {
        libraryManager = new LibraryManagerImpl(mediumCollection, categoryCollection, login, password);
        return libraryManager;
    }

    @Override
    public void contextInitialized(ServletContextEvent event) {
        System.out.println("Servelt initialized");
        parseProperty();
        ServletContext servletContext = event.getServletContext();

        try {
            servletContext.setAttribute("mediumManager", mediumManagerSetUp());
            servletContext.setAttribute("categoryManager", categoryManagerSetUp());
            servletContext.setAttribute("libraryManager", libraryManagerSetUp());

            logger.info("Managers successfully created.");
        } catch (DatabaseException ex) {
            logger.debug("Error initializing servlet context");
        }
    }

    @Override
    public void contextDestroyed(ServletContextEvent event) {

        mediumManager = null;
        categoryManager = null;
        libraryManager = null;

        System.out.println("Servlet ended");
    }
}
